package calvin.study.rabbitmq.base;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMqBase {

	private static ConnectionFactory connectionFactory;
	
	private static Connection conn = null;
	private static Channel channel = null;

	public static ConnectionFactory getConnectionFactory() {
		if (null == connectionFactory) {
			connectionFactory = new ConnectionFactory();
			connectionFactory.setHost("127.0.0.1");
			connectionFactory.setPort(5672);
			connectionFactory.setUsername("Calvin");
			connectionFactory.setPassword("123qwe");
			connectionFactory.setVirtualHost("/");
		}
		return connectionFactory;
	}

	public static Channel getChannel() {
		try {
			ConnectionFactory factory = getConnectionFactory();
			conn = factory.newConnection();
			channel = conn.createChannel();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return channel;
	}
	
	public static void close() {
		try {
			channel.close();
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}